<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\FormVal;
use backend\modules\supplier\models\Factory;
use yii\helpers\ArrayHelper;
use kitt2000\widgets\Ztree;
use backend\modules\supplier\models\GoodsClass;

/* @var $this yii\web\View */
/* @var $model common\models\goods\GoodsSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="goods-search well">

    <?php
    $form = ActiveForm::begin([
                'action' => ['index'],
                'method' => 'get',
    ]);

    $factorys = ArrayHelper::map(Factory::getAll(), 'id', 'name');

    ?>


    <?php // $form->field($model, 'id')  ?>

    <h3 class="text-info">高级搜索</h3>
    <hr >

    <div class="row">
        <div class="col-sm-6">
            <div class="row">
                <div class="col-sm-6"><?= $form->field($model, 'name'); ?></div>
                <div class="col-sm-6"><?php echo $form->field($model, 'factory_id')->dropDownList($factorys, ['prompt' => '请选择']) ?></div>
            </div>
            <div class="row">
                <div class="col-sm-3"><?= $form->field($model, 'is_stock_disable')->dropDownList(FormVal::$bool, ['prompt' => '请选择']); ?></div>
                <div class="col-sm-3"><?= $form->field($model, 'is_auto_sale_on')->dropDownList(FormVal::$bool, ['prompt' => '请选择']); ?></div>
                <div class="col-sm-3"><?= $form->field($model, 'is_auto_sale_off')->dropDownList(FormVal::$bool, ['prompt' => '请选择']); ?></div>
                <div class="col-sm-3"><?= $form->field($model, 'is_on_sale')->dropDownList([1 => '上架',0 => '下架'],['prompt' => '请选择']);?></div>
            </div>
        </div>
        <div class="col-sm-6">
        <?php
        $setting = [
            'check' => ['enable' => true],
            'data' => ['simpleData' => ['enable' => TRUE]],
            'callback' => [
                'onCheck' => new \yii\web\JsExpression('function(event, treeId, treeNode){addClassId(treeId,treeNode);}')
            ],
        ];
     
        $selected = !empty($model->class_id) ? explode(',', $model->class_id) : [];
        $nodes = GoodsClass::getZtree($selected);

        echo $form->field($model, 'class_id')->widget(Ztree::className(), ['pluginOptions' => ['setting' => $setting, 'nodes' => $nodes]]);
        ?>
        </div>
    </div>


    <div class="form-group">
    <?= Html::submitButton('搜索', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('重置', ['class' => 'btn btn-default']) ?>
    </div>

        <?php ActiveForm::end(); ?>

</div>

<script>
    function addClassId(treeId,treeNode) {
        var ids=new Array();
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandNode(treeNode, true, true, true);
        
        var nodes = treeObj.getCheckedNodes(true);

        for (var i = 0; i < nodes.length; i++) {
            if(!nodes[i].isParent){
                ids.push(nodes[i].id);
            }
        }
        $('#goodssearch-class_id').val(ids.join(','));
    }
    
</script>